// pages/login/index.js
const {apiUrl} = getApp().globalData
import {getMsg, CptShow, UsersLogin} from "../../http/interface.js"
const router = require("../../utils/navigator.js");

const util = require('../../utils/util.js')
var obtainTime = null //倒计时函数
Page({

    /**
     * 页面的初始数据
     */
    data: {
        phone: '',
        code: "",
        smscode: "",
        src: '',
        itemId: 0,
        uuid: '',
        time: '获取验证码', //倒计时
        currentTime: 61,
        type:""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      if(options.type){
        this.setData({
          type:options.type
        })
      }
        // this.getImg();
        this.get_CptShow();
    },
    getuuid() {
        this.setData({
            uuid: util.wxuuid()
        })
    },
    // getImg(){
    //   var timestamp = Date.parse(new Date());
    //   this.data.uuid = timestamp / 1000;
    //   this.setData({
    //     src: apiUrl +'/api/cpt/show?p=t&device_uuid='+this.data.uuid
    //   })
    // },
    // 获取图形验证码
    get_CptShow() {
        var _ = this;
        _.getuuid()
        CptShow(this.data.uuid).then(res => {
            if (res.status == 1) {
                console.log(res.data)
                _.setData({
                    src: res.data
                })
            }
        })
    },
    focus(e) {
        console.log(e)
        var id = e.target.id;
        console.log(id);
        this.setData({
            itemId: id
        })
    },
    bindKeyInput(event) {
        this.setData({
            phone: e.detail.value
        })
    },
    bindKeyQrcode(event) {
        this.setData({
            code: e.detail.value
        })
    },
    bindKeyCode(event) {
        this.setData({
            smscode: e.detail.value
        })
    },
    /**
     * 登录接口
     *
     */
    login() {
        var that = this;
        if (this.data.phone == '') {
            return wx.showToast({
                icon: 'none',
                title: '请输入正确的手机号码',
                duration: 1000,
            })
        }
        if (this.data.code == '') {
            return wx.showToast({
                icon: 'none',
                title: '请输入图形验证码',
                duration: 1000,
            })
        }
        if (this.data.smscode == '') {
            return wx.showToast({
                icon: 'none',
                title: '请输入短信验证码',
                duration: 1000,
            })
        }
        UsersLogin(that.data.phone, that.data.smscode).then(res => {
            if (res.status == 1) {
                wx.setStorage({
                    key: "api_token",
                    data: res.data.api_token
                })
                if(this.type==1){
                  wx.navigateTo({
                    url: '/pages/web/index?type=1',
                  })
                  return false;
                }
              if (this.type == 2) {
                wx.navigateTo({
                  url: '/pages/web/index?type=2',
                })
                return false;
              }
                router.switchTab('../user/index')
            } else {
                return wx.showToast({
                    icon: 'none',
                    title: res,
                    duration: 1000,
                })
            }
        })
    },
    // 获取手机号值
    getphone(e) {
        this.setData({
            phone: e.detail.value
        })
    },
    // 获取验证码值
    getsmscode(e) {
        this.setData({
            smscode: e.detail.value,
        });
    },
    phoneCodeBlur(e) {
        //手机验证码input输入框失焦
        // if (!this.data.sendF) {
        //     return wx.showToast({
        //         icon: 'none',
        //         title: '请输入获取验证码',
        //         duration: 1000,
        //     })
        // }
        let that = this;
        // if (e.detail.value.length != 4 && e.detail.value.length != 0) {
        //   wx.showToast({
        //     icon: 'none',
        //     title: '请输入正确的手机验证码',
        //     duration: 1000,
        //   })
        // }
        // that.setData({
        //     smscode: e.detail.value,
        // });
    },
    // 获取短信验证码
    getCode() {
        var that = this;
        if (this.data.phone == '') {
            return wx.showToast({
                icon: 'none',
                title: '请输入手机号',
                duration: 1000,
            })
        }
        if (this.data.phone.length != 11 && this.data.phone[0] != 1) {
            return wx.showToast({
                icon: 'none',
                title: '请输入正确的手机号码',
                duration: 1000,
            })
        }
        if (this.data.code == '') {
            return wx.showToast({
                icon: 'none',
                title: '请输入图形验证码',
                duration: 1000,
            })
        }
        // if(this.data.code.length!=5){
        //   return wx.showToast({
        //     icon: 'none',
        //     title: '请输入正确的图形验证码',
        //     duration: 1000,
        //   })
        // }
        that.getVerificationCode();
        that.setData({
            disabled: true
        });
        getMsg(this.data.phone, this.data.uuid, this.data.code).then(res => {
            console.log(res);

            if (res.status == 1) {
                wx.showToast({
                    icon: 'none',
                    title: '短信发送成功！',
                    duration: 1500,
                });

            } else {
                wx.showToast({
                    icon: 'none',
                    title: res.msg,
                    duration: 2000,
                });
            }
        })
    },
    getVerificationCode() {
        var that = this;
        var currentTime = that.data.currentTime
        obtainTime = setInterval(function () {
            currentTime--;
            that.setData({
                time: currentTime + '秒'
            })
            if (currentTime <= 0) {
                clearInterval(obtainTime)
                that.setData({
                    time: '重新发送',
                    currentTime: 61,
                    disabled: false
                })
            }
        }, 1000)
    },
    /**
     * check验证码
     */
    checkPhone(e) {
        let that = this;
        if (e.detail.value.length != 11 || e.detail.value[0] != 1) {
            wx.showToast({
                icon: 'none',
                title: '请输入正确的手机号码',
                duration: 1000,
            })
        }
        ;
    },
    checkMsg(e) {
        let that = this;
        if (e.detail.value.length != 11 || e.detail.value[0] != 1) {
            wx.showToast({
                icon: 'none',
                title: '请输入正确的新手机号码',
                duration: 1000,
            })
        }
        ;
        that.setData({
            phone: e.detail.value
        })
    },
    cptBlur(e) {
        //图形验证码input输入框失焦
        let that = this;
        if (e.detail.value.length != 4 && e.detail.value.length != 0) {
            wx.showToast({
                icon: 'none',
                title: '请输入正确的图形验证码',
                duration: 1000,
            })
        }
        that.setData({
            code: e.detail.value,
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})